@page "/admin/change-password"

@using MyBlogServer.Services
@inject UserService UserService
@inject CustomAuthenticationStateProvider AuthStateProvider
@inject NavigationManager Navigation

@layout AdminLayout
@rendermode InteractiveServer

<PageTitle>修改密码</PageTitle>

<AdminRouteTemplate>
    <div class="admin-container">
        <div class="row justify-content-center">
            <div class="col-md-8 col-lg-6">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title mb-0">修改密码</h3>
                    </div>
                    <div class="card-body">
                        @if (!string.IsNullOrEmpty(_message))
                        {
                            <div class="alert alert-success">@_message</div>
                        }

                        @if (!string.IsNullOrEmpty(_errorMessage))
                        {
                            <div class="alert alert-danger">@_errorMessage</div>
                        }

                        <EditForm Model="@_changePasswordModel" OnValidSubmit="HandleValidSubmit">
                            <DataAnnotationsValidator />
                            <ValidationSummary />

                            <div class="mb-3">
                                <label for="currentPassword" class="form-label">当前密码</label>
                                <InputText id="currentPassword" type="password" class="form-control" @bind-Value="_changePasswordModel.CurrentPassword" />
                            </div>

                            <div class="mb-3">
                                <label for="newPassword" class="form-label">新密码</label>
                                <InputText id="newPassword" type="password" class="form-control" @bind-Value="_changePasswordModel.NewPassword" />
                            </div>

                            <div class="mb-3">
                                <label for="confirmPassword" class="form-label">确认新密码</label>
                                <InputText id="confirmPassword" type="password" class="form-control" @bind-Value="_changePasswordModel.ConfirmPassword" />
                            </div>

                            <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                                <button type="submit" class="btn btn-primary">修改密码</button>
                                <button type="button" class="btn btn-secondary" @onclick="Cancel">取消</button>
                            </div>
                        </EditForm>
                    </div>
                </div>
            </div>
        </div>
    </div>
</AdminRouteTemplate>

@code {
    private ChangePasswordModel _changePasswordModel = new();
    private string _message = string.Empty;
    private string _errorMessage = string.Empty;

    // 获取当前用户名
    private string GetCurrentUsername()
    {
        // 在实际应用中，您会从认证状态中获取当前用户
        return "admin";
    }

    public class ChangePasswordModel
    {
        public string CurrentPassword { get; set; } = string.Empty;
        public string NewPassword { get; set; } = string.Empty;
        public string ConfirmPassword { get; set; } = string.Empty;
    }

    private async Task HandleValidSubmit()
    {
        try
        {
            var username = GetCurrentUsername();
            
            // 检查新密码和确认密码是否匹配
            if (_changePasswordModel.NewPassword != _changePasswordModel.ConfirmPassword)
            {
                _errorMessage = "新密码和确认密码不匹配";
                return;
            }
            
            // 检查新密码是否与当前密码相同
            if (_changePasswordModel.NewPassword == _changePasswordModel.CurrentPassword)
            {
                _errorMessage = "新密码不能与当前密码相同";
                return;
            }

            // 更新密码
            var success = await UserService.ChangePasswordAsync(username, _changePasswordModel.CurrentPassword, _changePasswordModel.NewPassword);
            if (success)
            {
                _message = "密码修改成功";
                _errorMessage = string.Empty;
                
                // 清空表单
                _changePasswordModel = new ChangePasswordModel();
            }
            else
            {
                _errorMessage = "当前密码不正确";
                _message = string.Empty;
            }
        }
        catch (Exception ex)
        {
            _errorMessage = $"修改密码时发生错误: {ex.Message}";
            _message = string.Empty;
        }
    }

    private void Cancel()
    {
        Navigation.NavigateTo("/admin/posts");
    }
}